package com.xit.cloud.service;

import com.sun.org.apache.regexp.internal.RE;
import com.vmware.vim25.VirtualMachineCapability;
import com.vmware.vim25.VirtualMachineConfigInfo;
import com.vmware.vim25.mo.*;
import com.xit.cloud.vm.SampleUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author malguy-wang sir
 * @create ---
 */
@Repository
public class vmService {
    public List show() throws Exception {
        ServiceInstance si = SampleUtil.createServiceInstance();
        long end = System.currentTimeMillis();
        Folder rootFolder = si.getRootFolder();
        String name = rootFolder.getName();
        System.out.println("vc root folder:" + name);
        ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");
        if (mes == null || mes.length == 0) {
            return null;
        }

        VirtualMachine vms[] = new VirtualMachine[mes.length];
        VirtualMachineConfigInfo vminfos[] = new VirtualMachineConfigInfo[mes.length];
        VirtualMachineCapability vmcs[] = new VirtualMachineCapability[mes.length];
        for (int i = 0; i < mes.length; i++) {
            vms[i] = (VirtualMachine) mes[i];//获取第i给虚拟机
            vminfos[i] = vms[i].getConfig();
            vmcs[i] = vms[i].getCapability();
        }
        List show = new ArrayList();
        for (int i = 0; i < vminfos.length; i++) {
            show.add(vminfos[i].getName());
            show.add(vminfos[i].getGuestFullName());
        }
        for (VirtualMachineConfigInfo vminfo : vminfos) {
            System.out.println("GuestOS: " + vminfo.getGuestFullName());
            System.out.println("Name: "+vminfo.getName());
        }
        si.getServerConnection().logout();
        return show;
    }
}
